<?php
	FLEA::loadClass('FLEA_Db_TableDataGateway');

	class	Model_Practitioner	extends		FLEA_Db_TableDataGateway
	{
		var		$tableName		=	'practitioner';
		var		$primaryKey		=	'PractitionerID';
	
		/**
		 * get practitioners of the fimr
		 *
		 * @param unknown_type $firmID
		 * @return unknown
		 */
		function	findAllPractitioners($firmID)
		{
			$sql	=	
				"
				select practitioner.* , user.FirstName, user.LastName , user.Email,
				user.Address,user.City,user.State,user.Country,user.PostalCode,
				user.WorkPhone,user.HomePhone,user.CellPhone from user,practitioner
				where practitioner.UserID=user.UserID and 
				practitioner.FirmID=".$firmID;
			$practitioners	=	$this->findBySql($sql);
			return $practitioners;
		}
		
		function	findPractitioner($practitionerID)
		{
			$sql	=	
				"
				select practitioner.* , user.FirstName, user.LastName , user.Email,
				user.Address,user.City,user.State,user.Country,user.PostalCode from user,practitioner
				where practitioner.UserID=user.UserID and 
				practitioner.PractitionerID=".$practitionerID;
			$practitioners	=	$this->findBySql($sql);
			return $practitioners[0];
		}
		
		function	deletePractitioner($practitionerID)
		{
			$dbo	=	FLEA::getDBO();
			
			$sql	=	"delete from user where UserID in (select UserID from practitioner where PractitionerID=".$practitionerID.")";
			$dbo->execute($sql);
			
			$sql	=	"delete from practitioner where PractitionerID=".$practitionerID;
			$dbo->execute($sql);
			$sql	=	"update invention set PractitionerID=0 where PractitionerID=".$practitionerID;
			$dbo->execute($sql);
			
			
			return 1;
		}
		
		
		function	practitionerRegister($firmID,$email,$firstName,$lastName,
			$bAttorney = 0 ,$bAgent = 0, $bPatentBar= 0 ,$bEditor = 0, $bDrafaman =0)
		{
			$_modelUser				=&	FLEA::getSingleton('Model_User');
	
			$user					=	$_modelUser->findByField("Email",$email);
			if($user["Email"]	==	$email)
			{
				/**
				 * The email is used,try other!
				 */
				return -1;
			}
			else
			{
				$user["Email"]				=	$email;
				$user["Password"]			=	$email;
				$user['FirstName']			=	$firstName;
				$user['LastName']			=	$lastName;
	
				$userID						=	$_modelUser->create($user);
	
				$practitioner['UserID']		=	$userID;
				$practitioner['FirmID']		=	$firmID;
				
				$practitioner['BAttorney']	=	($bAttorney == '')?0:1;
				$practitioner['BAgent']		=	($bAgent == '')?0:1;			
				$practitioner['PatentBar']	=	($bPatentBar == '')?0:1;			
				$practitioner['Editor']		=	($bEditor=='')?0:1;						
				$practitioner['Draftsman']	=	($bDrafaman=='')?0:1;
				
				$practitionerID				=	$this->create($practitioner);
	
				require("Extra/Mail.php");
				set_time_limit(100);
	
				$SmtpEmailTo = $Email;
	
				$mailsubject 	= 	"[IP Manager] ------";
				$mailHeader		=	"Content-Type:text/HTML ";
	
				$mailBody		=	"<div style='font-family: \"Times New Roman\", Times, serif;color:#000000;font-size:14px;
				font-weight:800;'>
				You are invited to join our online docketing system. <br><br> 
				Your password is the same as your email.<br><br>
				Please click  
				<a href='".url('Default','PractitionerLogin',array('Email'=>$Email))."'>here</a> to login.<br><br>
				IP Manager Firm : ".$this->viewdata['firm']['Name']."
				</div>";
	
				$smtp = new smtp(SmtpServer,Smtpserverport,true,SmtpUser,SmtpPass);
				$smtp->debug = true;
	
	
				$re				=
				$smtp->sendmail($SmtpEmailTo,SmtpUserMail,$mailsubject,$mailBody,"","","",$mailHeader);
				if($re)
				{
					/**
					 * Invitation has been sent,and send email to him
					 */
					return 1;
				}
				else
				{
					/**
					 * Invitation has been sent,but send email to him failly
					 */
					return 2;
				}
			}
		}
	}
?>